package com.chat.view;

import com.chat.client.ClientChat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class loginView extends JFrame {
    
    public ClientChat client = null;

    public void init(){

        this.setTitle("聊天客户端");
        //设置窗口大小和初始位置
        this.setBounds(700,400,280,200);
        //设置窗口不可改变
        this.setResizable(false);
        //设置窗口关闭按钮点击后关闭窗口
        this.setDefaultCloseOperation(3);

        JPanel panel = new JPanel();
        //设置布局模式为绝对布局
        panel.setLayout(null);

        //服务器地址
        JLabel SLabel = new JLabel("服务器地址：");
        SLabel.setBounds(10,15,80,30);
        panel.add(SLabel);
        //服务器地址
        JTextField SText = new JTextField();
        SText.setBounds(90,15,150,30);
        SText.setText("127.0.0.1:9700");
//        SText.setText("49.232.169.224:9700");
        panel.add(SText);

        //用户名标签
        JLabel userLabel = new JLabel("用户名：");
        userLabel.setBounds(35,55,60,30);
        panel.add(userLabel);
        //用户名
        JTextField userText = new JTextField();
        userText.setBounds(90,55,150,30);
        userText.requestFocus();
        panel.add(userText);

        //密码标签
        JLabel PLabel = new JLabel("密码：");
        PLabel.setBounds(47,95,50,30);
        panel.add(PLabel);
        //密码框
        JTextField PText = new JPasswordField();
        PText.setBounds(90,95,150,30);
        PText.setColumns(16);
//        PText.setText("123456");
        panel.add(PText);

        JButton login = new JButton("登录");
        login.setBounds(50,135,80,30);
        panel.add(login);

        JButton register = new JButton("注册");
        register.setBounds(160,135,80,30);
        panel.add(register);

        SText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==10){
                    userText.requestFocus();
                }
            }
        });
        
        userText.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e){
                //当按下Enter键时，插入一个换行符(Enter键的keycode为10)
                if(e.getKeyCode()==10){
                   PText.requestFocus();
                }
            }

        });
        
        PText.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e){
                //当按下Enter键时，插入一个换行符(Enter键的keycode为10)
                if(e.getKeyCode()==10){
                    String server = SText.getText().trim();
                    String user = userText.getText().trim();
                    String pass = PText.getText().trim();

                    try {

                        loginin(server,user,pass);

                    } catch (IOException | ClassNotFoundException ioException) {
                        ioException.printStackTrace();
                        JOptionPane.showMessageDialog(new JPanel(), "连接服务失败。", "错误",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }

        });
        
        //登录
        login.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String server = SText.getText().trim();
                String user = userText.getText().trim();
                String pass = PText.getText().trim();

                try {

                    loginin(server,user,pass);

                } catch (IOException | ClassNotFoundException ioException) {
                    ioException.printStackTrace();
                    JOptionPane.showMessageDialog(new JPanel(), "连接服务失败。", "错误",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        //注册
        register.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String server = SText.getText().trim();

                new registerView(server).init();
                loginView.this.setVisible(false);
            }
        });


        this.add(panel);
        this.setVisible(true);
    }
    
    public void loginin(String server,String user,String pass) throws IOException, ClassNotFoundException {

        if (server.equals("") || server.length() > 30 || server.indexOf(":") == -1)
        {
            JOptionPane.showMessageDialog(new JPanel(), "服务器格式错误。", "错误",JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (user.equals("") || user.matches("/\\w\\u4e00-\\u9fa5/") || user.length() > 16)
        {
            JOptionPane.showMessageDialog(new JPanel(), "用户名格式错误。", "错误",JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (pass.equals("") || !pass.matches("^\\w+$") || user.length() > 16)
        {
            JOptionPane.showMessageDialog(new JPanel(), "密码格式错误。", "错误",JOptionPane.WARNING_MESSAGE);
            return;
        }


        String[] split = server.split(":");

        client = new ClientChat(split[0], Integer.parseInt(split[1]));
        String news = client.putNews("login", user + "\t" + pass);
        System.out.println(news);

        switch (news.trim())
        {
            case "exist":
                JOptionPane.showMessageDialog(new JPanel(), "用户已经登录", "提示",JOptionPane.WARNING_MESSAGE);
                break;
            case "ufail":
                JOptionPane.showMessageDialog(new JPanel(), "用户不存在，请先注册。", "提示",JOptionPane.WARNING_MESSAGE);
                break;
            case "pfail":
                JOptionPane.showMessageDialog(new JPanel(), "密码错误请重试。", "提示",JOptionPane.WARNING_MESSAGE);
                break;
            default:
                client.setUser(user);
                client.setId(Integer.parseInt(news.trim()));
                new ClientView(client).start();
                loginView.this.setVisible(false);
                break;
        }

    }
    


    public static void main(String[] args) {

        new loginView().init();


//        new ClientView().init();

    }

}
